import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class View extends JFrame {
	
	int width; 				//Number of Pixels to render on the x axis;
	int height; 			//Number of Pixels to render on the y axis;
	boolean fullscreen; 	//Is full screen mode on?
	boolean verbose; 		//Is verbose mode on?
	int row; 				//Number of rows
	int column; 			//Number of columns 
	RenderableBox[][] theBoxes; //The renderable boxes of fractal goodness
	
	public View(Properties prop) {
		
		//Initialise Variables
		super("Buddhabrot");
		verbose = Boolean.parseBoolean(prop.getProperty("verbose"));
		fullscreen = Boolean.parseBoolean(prop.getProperty("fullscreen"));
		
		if(verbose) System.out.println("width = " + width + " height = " + height);
		row = Integer.parseInt(prop.getProperty("row"));
		column = Integer.parseInt(prop.getProperty("column"));
		
		theBoxes = new RenderableBox[row][column];
		
		int boxWidth = Integer.parseInt(prop.getProperty("boxWidth"));
		int boxHeight = Integer.parseInt(prop.getProperty("boxHeight"));
		int lastBoxWidth = Integer.parseInt(prop.getProperty("lastBoxWidth"));
		int lastBoxHeight = Integer.parseInt(prop.getProperty("lastBoxHeight"));
		
		//Set up the panels and layouts
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new GridLayout2(row, column, 0, 0));
		
		for(int i = 0; i<row; i++){
			for(int j = 0; j<column; j++){
				if(i != row-1 && j != column-1){
					theBoxes[i][j] = new RenderableBox(boxWidth, boxHeight, verbose);
				}
				else if(i != row-1 && j == column-1){
					theBoxes[i][j] = new RenderableBox(lastBoxWidth, boxHeight, verbose);
				}
				else if(i == row-1 && j != column-1){
					theBoxes[i][j] = new RenderableBox(boxWidth, lastBoxHeight, verbose);
				}					
				else{
					theBoxes[i][j] = new RenderableBox(lastBoxWidth, lastBoxHeight,verbose);
				}
				mainPanel.add(theBoxes[i][j]);
			}
		}		
		this.setContentPane(mainPanel);
		this.getContentPane().setBackground(Color.black);
        this.pack();
		this.setResizable(false);
		this.setMaximumSize(new Dimension(width, height));
		if(fullscreen) this.setLocation(-3,-22);  
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	}	
	
	public void replaceBox(int x, int y, BufferedImage newImage){
		theBoxes[x][y].setBufferedImage(newImage);
		theBoxes[x][y].repaint();
	}
	
}
